﻿namespace Zaggit.UI
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using Zaggit.Core;
    using System.Text;
    using Zaggit.DomainObjects;
    using System.Net.Sockets;
    
    public partial class Main : Form
    {

        #region Fields

        private static Messages _Window;

        private static IMessager Zaggit;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Main" /> class.
        /// </summary>
        public Main()
        {
            InitializeComponent();
            var s = new Properties.Settings();
            try
            {
                Zaggit = new Messager(s.Port, new UTF32Encoding(), Crypto.GetInstance());
            }
            catch (SocketException)
            {
                MessageBox.Show(string.Format("Zaggit could not attach to port {0}.  Please check there are no other copies of Zaggit running.", s.Port), string.Format("Port {0} in use", s.Port), MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
                return;
            }

            Zaggit.MessageRecieved += AlertMessage;
            this.Hide();
            _Window = new Messages(ref Zaggit);
            _Window.Show();
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Alerts the message.
        /// </summary>
        /// <param name="message">The message.</param>
        private void AlertMessage(UserMessage message)
        {
            if (_Window == null || _Window.Visible) return;
            if (message.Type == MessageType.KeepAlive || message.Type == MessageType.Typing) return;

            Action handleMessageAlert = () =>
            {
                TrayIcon.Icon = Properties.Resources.zaggitAlert;
                this.Refresh();
                TrayIcon.BalloonTipClicked += TrayIcon_BalloonTipClicked;
                TrayIcon.ShowBalloonTip(5, "Zaggit", string.Format("{0}: {1}", message.Sender, message.Contents), ToolTipIcon.Info);
            };

            this.Invoke(handleMessageAlert);
        }

        /// <summary>
        /// Handles the FormClosed event of the Main control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="FormClosedEventArgs" /> instance containing the event data.</param>
        private void Main_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.TrayIcon.Dispose();
        }

        /// <summary>
        /// Handles the Shown event of the Main control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void Main_Shown(object sender, EventArgs e)
        {
            this.Hide();
        }

        /// <summary>
        /// Handles the Click event of the openMessageWindowToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void openMessageWindowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.TrayIcon_MouseDoubleClick(null, null);
        }

        /// <summary>
        /// Handles the Click event of the quitToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// Handles the BalloonTipClicked event of the TrayIcon control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void TrayIcon_BalloonTipClicked(object sender, EventArgs e)
        {
            Action handleClick = () =>
            {
                this.TrayIcon.Icon = Properties.Resources.zaggit;
                this.Refresh();
                _Window.Show();
            };

            this.Invoke(handleClick);
        }

        /// <summary>
        /// Handles the MouseDoubleClick event of the TrayIcon control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
        private void TrayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            TrayIcon_BalloonTipClicked(null, null);
        }

        #endregion

    }
}
